﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Module.Common.Models;
using Module.Core.EntitiesInfo;

namespace Module.Core.DAO
{
    public class UpgradeAccountDAO
    {
        #region Fields

        protected string connectionStringName;

        #endregion

        #region Methods

        /// <summary>
        /// Saves a record to the UpgradeAccount table.
        /// </summary>
        public virtual int Insert(UpgradeAccountInfo upgradeAccountInfo)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdMember", upgradeAccountInfo.IdMember),
				new SqlParameter("@CreatedOn", upgradeAccountInfo.CreatedOn),
				new SqlParameter("@Price", upgradeAccountInfo.Price),
				new SqlParameter("@Status", upgradeAccountInfo.Status)
			};

            return (int)SqlClientUtility.ExecuteScalar(connectionStringName, CommandType.StoredProcedure, "UpgradeAccount_Insert", parameters);
        }

        /// <summary>
        /// Updates a record in the UpgradeAccount table.
        /// </summary>
        public virtual void Update(UpgradeAccountInfo upgradeAccountInfo)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@Id", upgradeAccountInfo.Id),
				new SqlParameter("@IdMember", upgradeAccountInfo.IdMember),
				new SqlParameter("@CreatedOn", upgradeAccountInfo.CreatedOn),
				new SqlParameter("@Price", upgradeAccountInfo.Price),
				new SqlParameter("@Status", upgradeAccountInfo.Status)
			};

            SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "UpgradeAccount_Update", parameters);
        }

        /// <summary>
        /// Cập nhật status khi khách hàng nâp cấp
        /// price=2000->status=3  -khách hàng bạc
        /// price=4000->status=2  -Khách hàng vàng
        /// price=6000->status=1  -Khách hàng kim cương
        /// 
        /// </summary>
        public virtual void UpdateStatus(int idMember, int status)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdMember", idMember),
                new SqlParameter("@Status", status),
			};

            SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "UpgradeAccount_UpdateStatus", parameters);
        }

        /// <summary>
        /// Deletes a record from the UpgradeAccount table by its primary key.
        /// </summary>
        public virtual void Delete(int id)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@Id", id)
			};

            SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "UpgradeAccount_Delete", parameters);
        }

        /// <summary>
        /// Selects a single record from the UpgradeAccount table.
        /// </summary>
        public virtual UpgradeAccountInfo Select(int status, string param)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@Status", status),
                new SqlParameter("@Param", param)
                
			};

            using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "UpgradeAccount_Select", parameters))
            {
                if (dataReader.Read())
                {
                    return MakeUpgradeAccountInfo(dataReader);
                }
                else
                {
                    return null;
                }
            }
        }
        #region danh sách thành viên nâng cấp

        public virtual int SelectCount(string codeMember,string email)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{           
         
               new SqlParameter("@CodeMember", codeMember),
               new SqlParameter("@Email", email),
               new SqlParameter("@IsTotalRow", 1)
			};

            return (int)SqlClientUtility.ExecuteScalar(connectionStringName, CommandType.StoredProcedure, "Member_SelectLstNangcap", parameters);
        }

        public virtual DataTable Search(string codeMember,string email,int beginRow, int endRow, string orderByColumn)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{
                new SqlParameter("@CodeMember", codeMember),
                new SqlParameter("@Email", email),
                new SqlParameter("@BeginRow", beginRow),
                new SqlParameter("@EndRow", endRow),
                new SqlParameter("@OrderByColumn", orderByColumn),
				new SqlParameter("@isTotalRow", 0)
			};

            return SqlClientUtility.ExecuteDataTable(connectionStringName, CommandType.StoredProcedure, "Member_SelectLstNangcap", parameters);
        }
        #endregion

        public virtual int SumPrice(string status, string param)
        {
            int tong = 0;
            SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@Status", status),
				new SqlParameter("@Param", param)
			};
            DataTable dt = SqlClientUtility.ExecuteDataTable(connectionStringName, CommandType.StoredProcedure, "UpgradeAccount_Select", parameters);
            if (dt != null && dt.Rows.Count > 0)
            {
                if (dt.Rows[0]["tongtien"].ToString() != null && dt.Rows[0]["tongtien"].ToString() != "")
                    tong = int.Parse(dt.Rows[0]["tongtien"].ToString());
            }
            else
            {
                tong = 0;
            }
            return tong;
        }

        /// <summary>
        /// Selects all records from the UpgradeAccount table.
        /// </summary>
        public CHRTList<UpgradeAccountInfo> SelectAll()
        {
            using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "UpgradeAccount_SelectAll"))
            {
                CHRTList<UpgradeAccountInfo> upgradeAccountInfoList = new CHRTList<UpgradeAccountInfo>();
                while (dataReader.Read())
                {
                    UpgradeAccountInfo upgradeAccountInfo = MakeUpgradeAccountInfo(dataReader);
                    upgradeAccountInfoList.Add(upgradeAccountInfo);
                }

                return upgradeAccountInfoList;
            }
        }

        /// <summary>
        /// Creates a new instance of the UpgradeAccount class and populates it with data from the specified SqlDataReader.
        /// </summary>
        protected virtual UpgradeAccountInfo MakeUpgradeAccountInfo(SqlDataReader dataReader)
        {
            UpgradeAccountInfo upgradeAccountInfo = new UpgradeAccountInfo();
            upgradeAccountInfo.Id = SqlClientUtility.GetInt32(dataReader, DbConstants.UPGRADEACCOUNT.ID, 0);
            upgradeAccountInfo.IdMember = SqlClientUtility.GetInt32(dataReader, DbConstants.UPGRADEACCOUNT.IDMEMBER, 0);
            upgradeAccountInfo.CreatedOn = SqlClientUtility.GetDateTime(dataReader, DbConstants.UPGRADEACCOUNT.CREATEDON, DateTime.Now);
            upgradeAccountInfo.Price = SqlClientUtility.GetInt32(dataReader, DbConstants.UPGRADEACCOUNT.PRICE, 0);
            upgradeAccountInfo.Status = SqlClientUtility.GetInt32(dataReader, DbConstants.UPGRADEACCOUNT.STATUS, 0);

            return upgradeAccountInfo;
        }

        #endregion
    }
}
